{$CLEO .cs}
{$USE CLEO+}
{$USE Audio}
{$USE newOpcodes}
{$USE ini}

0000: NOP

script_name "GangsWithMusic"

// By ArtemQa146

//  

wait 250

if
    0AA2: 31@ = load_dynamic_library "CLEO+.cleo"
then
    if
        0AA4: 30@ = get_dynamic_library_procedure "GetCleoPlusVersion" library 31@
    then
        0AA7: call_function_return 30@ num_params 0 pop 0 29@
        if
            not 29@ >= 0x1020000
        then
            0AD1: show_formatted_text_highpriority "Cleo+ 1.2.0 (or higher) version is required, the Busy Pedestrians mod is disabled" time 15000
            0AA3: free_dynamic_library 31@
            0A93: terminate_this_custom_script
        end
        0AA3: free_dynamic_library 31@
    end
else
    0AD1: show_formatted_text_highpriority "Cleo+ 1.2.0 (or higher) version is required, the Busy Pedestrians mod is disabled" time 15000
    0AA3: free_dynamic_library 31@
    0A93: terminate_this_custom_script
end

0AF0: 0@ = read_int_from_ini_file "cleo\Busy Pedestrians.ini" section "Settings" key "Gangs With Music"
if
    0@ == 0
then
    0A93: terminate_this_custom_script
end

if
    is_game_first_start
then
    0F00: load_special_model_dff "modelsq\Blaster_1" txd "modelsq\Blasters" store_to 0@
    0AF1: write_int 0@ to_ini_file "cleo\GangsWithMusic.ini" section "Models" key "Blaster_1"
    0F00: load_special_model_dff "modelsq\Blaster_2" txd "modelsq\Blasters" store_to 0@
    0AF1: write_int 0@ to_ini_file "cleo\GangsWithMusic.ini" section "Models" key "Blaster_2"
    0F00: load_special_model_dff "modelsq\Blaster_3" txd "modelsq\Blasters" store_to 0@
    0AF1: write_int 0@ to_ini_file "cleo\GangsWithMusic.ini" section "Models" key "Blaster_3"
    0F00: load_special_model_dff "modelsq\Blaster_4" txd "modelsq\Blasters" store_to 0@
    0AF1: write_int 0@ to_ini_file "cleo\GangsWithMusic.ini" section "Models" key "Blaster_4"
end

//  

const
    RenderObject = 31@
    BandActor = 0@
    BandCar = 29@
    AudioMusic = 30@
    Music = 28@
    VolumeActor = 27@
    VolumeCar = 26@
    Key_Skip = 25@
    model_1 = 24@
    model_2 = 23@
    model_3 = 22@
    model_4 = 21@
end


0AF0: model_1 = read_int_from_ini_file "cleo\GangsWithMusic.ini" section "Models" key "Blaster_1"
0AF0: model_2 = read_int_from_ini_file "cleo\GangsWithMusic.ini" section "Models" key "Blaster_2"
0AF0: model_3 = read_int_from_ini_file "cleo\GangsWithMusic.ini" section "Models" key "Blaster_3"
0AF0: model_4 = read_int_from_ini_file "cleo\GangsWithMusic.ini" section "Models" key "Blaster_4"

0AF0: Music = read_int_from_ini_file "cleo\Busy Pedestrians.ini" section "Gangs With Music" key "Number of songs"
Music += 1 //       .ini  
0AF2: VolumeActor = read_float_from_ini_file "cleo\Busy Pedestrians.ini" section "Gangs With Music" key "Max Volume Actor"
0AF2: VolumeCar = read_float_from_ini_file "cleo\Busy Pedestrians.ini" section "Gangs With Music" key "Max Volume Car"
0AF0: Key_Skip = read_int_from_ini_file "cleo\Busy Pedestrians.ini" section "Gangs With Music" key "Key Skip Song"

wait 100

while true
wait 100
if
    Player.Defined($PLAYER_CHAR)
then
    0A8D: 4@ = read_memory 0x86D284 size 4 virtual_protect 0 //     .  50.5
    00A0: store_actor $PLAYER_ACTOR position_to 1@ 2@ 3@
    if
        0AE1: BandActor = random_char_near_point 1@ 2@ 3@ in_radius 4@ find_next 1 pass_deads 1
    then
    
        //   
        if and
            80DF:   not actor BandActor driving
            not is_char_script_controlled BandActor
            not locate_char_distance_to_char $PLAYER_ACTOR char BandActor radius 40.0
        then
            089F: get_actor BandActor pedtype_to 4@
                if or
                    4@ == 7 // (Ballas)
                    4@ == 8 // (Grove Street Families)
                    4@ == 9 // (Los Santos Vagos)
                    4@ == 10 // (San Fierro Rifa)
                    4@ == 14 // (Varrio Los Aztecas)
                then
                    if or
                        02D8:   actor BandActor current_weapon == 0 //  
                        02D8:   actor BandActor current_weapon == 4 // 
                        02D8:   actor BandActor current_weapon == 5 // 
                        02D8:   actor BandActor current_weapon == 22 // Colt45
                        02D8:   actor BandActor current_weapon == 28 // Micro Uzi
                        02D8:   actor BandActor current_weapon == 32 // Tec9
                    then
                        //     
                        0209: 2@ = random_int_in_ranges 1 5
                        if 2@ == 1
                        then 0085: 1@ = model_1 // (int)
                        end
                        if 2@ == 2
                        then 0085: 1@ = model_2 // (int)
                        end
                        if 2@ == 3
                        then 0085: 1@ = model_3 // (int)
                        end
                        if 2@ == 4
                        then 0085: 1@ = model_4 // (int)
                        end
                        0F02: create_render_object_to_char_bone_from_special BandActor special_model 1@ bone 34 offset 0 0 0 rotation 0 0 0 store_to RenderObject
                        //  
                        0209: 1@ = random_int_in_ranges 1 Music
                        089F: get_actor BandActor pedtype_to 10@ //  Pedtype      
                            if or
                                10@ == 10 // (San Fierro Rifa)
                                10@ == 14 // (Varrio Los Aztecas)
                            then
                                10@ = 9 // (Los Santos Vagos)
                            end
                        0AD3: 5@v = string_format "GM\B_%d_%d.mp3" 10@ 1@ // 10@ - PedType, 1@ -   
                        0AC1: AudioMusic = load_audio_stream_with_3d_support 5@v
                        0AC4: link_3d_audio_stream AudioMusic to_actor BandActor
                        0AAD: set_audio_stream AudioMusic state 1
                        
                        //    
                        repeat
                        wait 0
                        if
                            056D: actor BandActor defined
                        then
                            //   ,    3       ,   
                            04C4: store_coords_to 1@ 2@ 3@ from_actor BandActor with_offset 0.0 0.0 0.0
                            0EB2: get_offset_from_camera_in_world_coords 0.0 0.0 0.0 store_to 4@ 5@ 6@
                            050A: 1@ = distance_between_XYZ 1@ 2@ 3@ and_XYZ 4@ 5@ 6@
                            1@ /= 50.0
                            0087: 2@ = VolumeActor //   .ini 
                            0063: 2@ -= 1@  // (float)
                                if
                                    2@ < 0.0
                                then
                                    2@ = 0.0
                                end
                            0ABC: set_audio_stream AudioMusic volume 2@
                            
                            //      
                            if and
                                0019:   TIMERA > 2000
                                10@ == 8 // (Grove Street Families)
                                0EE4: locate_char_distance_to_char $PLAYER_ACTOR char BandActor radius 5.0
                                0AB0:   is_key_pressed Key_Skip
                            then
                                0AAD: set_audio_stream AudioMusic state 2
                                0006: TIMERA = 0
                            end


                            //   
                            0AB9: get_audio_stream AudioMusic state_to 1@
                            if or
                                1@ == -1
                                1@ == 2
                            then
                                0AAE: remove_audio_stream AudioMusic
                                0209: 1@ = random_int_in_ranges 1 Music
                                0AD3: 5@v = string_format "GM\B_%d_%d.mp3" 10@ 1@
                                0AC1: AudioMusic = load_audio_stream_with_3d_support 5@v // IF and SET
                                0AC4: link_3d_audio_stream AudioMusic to_actor BandActor
                                0AAD: set_audio_stream AudioMusic state 1
                            end
                            
                            
                            //         ,   
                            if
                                00DF:   actor BandActor driving
                            then
                                BandCar = Actor.CurrentCar(BandActor)
                                    if
                                        00DB:   actor $PLAYER_ACTOR in_car BandCar
                                    then
                                        051E: 1@ = get_current_radio_station
                                            if
                                                not 1@ == 12 //  
                                            then
                                                0ABC: set_audio_stream AudioMusic volume 0.0
                                            end
                                        end
                                    end

                            //  -   
                            if or
                                0118:   actor BandActor dead
                                is_char_script_controlled BandActor
                            then
                                BandActor = -1
                            end
                        end
                    until 856D: not actor BandActor defined
                    0AAE: remove_audio_stream AudioMusic
                end
            end
                
                //   
                else
                    if and
                        00DF:   actor BandActor driving
                        not is_char_script_controlled BandActor
                        not locate_char_distance_to_char $PLAYER_ACTOR char BandActor radius 30.0
                        locate_char_distance_to_char $PLAYER_ACTOR char BandActor radius 50.0
                    then
                        089F: get_actor BandActor pedtype_to 4@
                            if or
                                4@ == 7 // (Ballas)
                                4@ == 8 // (Grove Street Families)
                                4@ == 9 // (Los Santos Vagos)
                                4@ == 10 // (San Fierro Rifa)
                                4@ == 14 // (Varrio Los Aztecas)
                            then
                             BandCar = Actor.CurrentCar(BandActor)
                             0209: 1@ = random_int_in_ranges 1 Music
                                089F: get_actor BandActor pedtype_to 10@ //  Pedtype      
                                    if or
                                        10@ == 10 // (San Fierro Rifa)
                                        10@ == 14 // (Varrio Los Aztecas)
                                    then
                                        10@ = 9 // (Los Santos Vagos)
                                    end
                                        0AD3: 5@v = string_format "GM\B_%d_%d.mp3" 10@ 1@ // 10@ - PedType, 1@ -   
                                        0AC1: AudioMusic = load_audio_stream_with_3d_support 5@v
                                        0AC5: link_3d_audio_stream AudioMusic to_car BandCar
                                        0AAD: set_audio_stream AudioMusic state 1

                        //    
                        repeat
                        wait 0
                        if
                            056E:  car BandCar defined
                        then
                            //   ,    3       ,   
                            0407: store_coords_to 1@ 2@ 3@ from_car BandCar with_offset 0.0 0.0 0.0
                            0EB2: get_offset_from_camera_in_world_coords 0.0 0.0 0.0 store_to 4@ 5@ 6@
                            050A: 1@ = distance_between_XYZ 1@ 2@ 3@ and_XYZ 4@ 5@ 6@
                            1@ /= 50.0
                            0087: 2@ = VolumeCar //   .ini 
                            0063: 2@ -= 1@  // (float)
                                if
                                    2@ < 0.0
                                then
                                    2@ = 0.0
                                end
                            0ABC: set_audio_stream AudioMusic volume 2@
                            
                            //      
                            if and
                                0019:   TIMERA > 2000
                                10@ == 8 // (Grove Street Families)
                                0EE5: locate_char_distance_to_car $PLAYER_ACTOR car BandCar radius 5.0
                                0AB0:   is_key_pressed Key_Skip
                            then
                                0AAD: set_audio_stream AudioMusic state 2
                                0006: TIMERA = 0
                            end

                            //   
                            0AB9: get_audio_stream AudioMusic state_to 1@
                            if or
                                1@ == -1
                                1@ == 2
                            then
                                0AAE: remove_audio_stream AudioMusic
                                0209: 1@ = random_int_in_ranges 1 Music
                                0AD3: 5@v = string_format "GM\B_%d_%d.mp3" 10@ 1@
                                0AC1: AudioMusic = load_audio_stream_with_3d_support 5@v // IF and SET
                                0AC5: link_3d_audio_stream AudioMusic to_car BandCar
                                0AAD: set_audio_stream AudioMusic state 1
                            end

                            //  -   
                            if or
                                0119:   car BandCar wrecked
                                00DB:   actor $PLAYER_ACTOR in_car BandCar
                                0E08: is_car_script_controlled BandCar
                                8ABE:   not car BandCar engine_on
                            then
                                BandCar = -1
                            end
                        end
                        until 856E:  not car BandCar defined
                        0AAE: remove_audio_stream AudioMusic
                    end
                end
            end
        end
    end
end

